<template>
  <el-row :gutter="40" class="panel-group">
    <el-col :xs="12" :sm="12" :lg="7" class="card-panel-col" style="margin-right: 30px; margin-left: 15px">
      <div class="card-panel" @click="handleSetLineChartData('newVisitis')">
        <div class="card-panel-icon-wrapper icon-alarm">
          <svg-icon icon-class="alarm" class-name="card-panel-icon"/>
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            设备告警总数
          </div>
          <count-to :start-val="0" :end-val="dashboard.totalFault" :duration="2600" class="card-panel-num"/>
          条
        </div>
      </div>
    </el-col>
    <el-col :xs="12" :sm="12" :lg="7" class="card-panel-col" style="margin-right: 30px; margin-left: 30px">
      <div class="card-panel" @click="handleSetLineChartData('messages')">
        <div class="card-panel-icon-wrapper icon-message">
          <svg-icon icon-class="charge" class-name="card-panel-icon"/>
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            总能耗
          </div>
          <count-to :start-val="0" :end-val="dashboard.totalEnergy" :decimals="2" :duration="3000"
                    class="card-panel-num"/>
          KWH
        </div>
      </div>
    </el-col>
    <el-col :xs="12" :sm="12" :lg="7" class="card-panel-col" style="margin-left: 30px">
      <div class="card-panel" @click="handleSetLineChartData('purchases')">
        <div class="card-panel-icon-wrapper icon-power">
          <svg-icon icon-class="power" class-name="card-panel-icon"/>
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            运行总功率
          </div>
          <count-to :start-val="0" :end-val="dashboard.totalPower" :decimals="3" :duration="3200"
                    class="card-panel-num"/>
          KW
        </div>
      </div>
    </el-col>
    <el-col :xs="12" :sm="12" :lg="7" class="card-panel-col" style="margin-right: 30px; margin-left: 15px">
      <div class="card-panel" @click="handleSetLineChartData('shoppings')">
        <div class="card-panel-icon-wrapper icon-people">
          <svg-icon icon-class="peoples" class-name="card-panel-icon"/>
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            系统在线人数
          </div>
          <count-to :start-val="0" :end-val="dashboard.online" :duration="3600" class="card-panel-num"/>
        </div>
      </div>
    </el-col>
    <el-col :xs="12" :sm="12" :lg="7" class="card-panel-col" style="margin-right: 30px; margin-left: 30px">
      <div class="card-panel" @click="handleSetLineChartData('shoppings')">
        <div class="card-panel-icon-wrapper icon-shopping">
          <svg-icon icon-class="aircondition" class-name="card-panel-icon"/>
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            空调当前风量
          </div>
          <count-to :start-val="0" :end-val="dashboard.airFlow" :duration="3600" class="card-panel-num"/>
        </div>
      </div>
    </el-col>
    <el-col :xs="12" :sm="12" :lg="7" class="card-panel-col" style="margin-left: 30px">
      <div class="card-panel" @click="handleSetLineChartData('shoppings')">
        <div class="card-panel-icon-wrapper icon-controller">
          <svg-icon icon-class="controllerWind" class-name="card-panel-icon"/>
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            送排风当前风量
          </div>
          <count-to :start-val="0" :end-val="dashboard.controllerFlow" :duration="3600" class="card-panel-num"/>
        </div>
      </div>
    </el-col>
  </el-row>
</template>

<script>
import CountTo from 'vue-count-to'
import {getDashboard} from '../../api/dashboard/Dashboard'

export default {
  components: {
    CountTo
  },
  data() {
    return {
      dashboard: {
        totalFault: 0,
        totalPower: 0,
        totalEnergy: 0,
        airFlow: 0,
        freshFlow: 0,
        exhaustFlow: 0,
        controllerFlow: 0,
        online: 0
      }
    }
  },
  created() {
    this.getDashboard()
  },
  methods: {
    handleSetLineChartData(type) {
      this.$emit('handleSetLineChartData', type)
    },
    getDashboard() {
      getDashboard().then(res => {
        const data = res.content
        if (data !== null && typeof data != 'undefined' && data.length > 0) {
          const d = data[0]
          if (d.totalFault != null) {
            this.dashboard.totalFault = d.totalFault
          }
          if (d.totalPower != null) {
            this.dashboard.totalPower = d.totalPower / 1000
          }

          if (d.totalEnergy != null) {
            this.dashboard.totalEnergy = d.totalEnergy
          }

          if (d.freshFlow != null) {
            this.dashboard.freshFlow = d.freshFlow
          }

          if (d.airFlow != null) {
            this.dashboard.airFlow = d.airFlow
          }

          if (d.exhaustFlow != null) {
            this.dashboard.exhaustFlow = d.exhaustFlow
          }
          if (d.online != null) {
            this.dashboard.online = d.online
          }
          if (d.controllerFlow != null) {
            this.dashboard.controllerFlow = d.controllerFlow
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.panel-group {
  margin-top: 18px;

  .card-panel-col {
    margin-bottom: 32px;
  }

  .card-panel {
    height: 108px;
    cursor: pointer;
    font-size: 12px;
    position: relative;
    overflow: hidden;
    color: #666;
    background: #fff;
    box-shadow: 4px 4px 40px rgba(0, 0, 0, .05);
    border-color: rgba(0, 0, 0, .05);

    &:hover {
      .card-panel-icon-wrapper {
        color: #fff;
      }

      .icon-people {
        background: #937004;
      }

      .icon-power {
        background: #40c9c6;
      }

      .icon-message {
        background: #36a3f7;
      }

      .icon-money {
        background: #f4516c;
      }

      .icon-alarm {
        background: #f4516c;
      }

      .icon-shopping {
        background: #031c9e
      }

      .icon-fresh {
        background: #9df585
      }

      .icon-exhaust {
        background: #9199c6
      }

      .icon-controller {
        background: #881df3
      }
    }

    .icon-people {
      color: #937004;
    }

    .icon-power {
      color: #40c9c6;
    }

    .icon-message {
      color: #36a3f7;
    }

    .icon-money {
      color: #f4516c;
    }

    .icon-alarm {
      color: #f4516c;
    }

    .icon-shopping {
      color: #031c9e
    }

    .icon-fresh {
      color: #9df585
    }

    .icon-exhaust {
      color: #9199c6
    }

    .icon-controller {
      color: #881df3
    }

    .card-panel-icon-wrapper {
      float: left;
      margin: 14px 0 0 14px;
      padding: 16px;
      transition: all 0.38s ease-out;
      border-radius: 6px;
    }

    .card-panel-icon {
      float: left;
      font-size: 48px;
    }

    .card-panel-description {
      float: right;
      font-weight: bold;
      margin: 26px;
      margin-left: 0px;

      .card-panel-text {
        line-height: 18px;
        color: rgba(0, 0, 0, 0.45);
        font-size: 16px;
        margin-bottom: 12px;
      }

      .card-panel-num {
        font-size: 20px;
      }
    }
  }
}

@media (max-width:550px) {
  .card-panel-description {
    display: none;
  }

  .card-panel-icon-wrapper {
    float: none !important;
    width: 100%;
    height: 100%;
    margin: 0 !important;

    .svg-icon {
      display: block;
      margin: 14px auto !important;
      float: none !important;
    }
  }
}
</style>
